using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEGalleryExtension
{
    [Themeable(true)]
    public class AlbumControl : WebControl
    {
        #region Properties

        private string folderIcon;
        public string FolderIcon
        {
            get { return folderIcon; }
            set { folderIcon = value; }
        }

        private int directoryID;
        public int DirectoryID
        {
            get { return directoryID; }
            set { directoryID = value; }
        }

        private int repeatColumns;
        public int RepeatColumns
        {
            get { return repeatColumns; }
            set { repeatColumns = value; }
        }
        
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            PhotoInformation pinfo = new PhotoInformation();
            DataTable table = pinfo.getDirectories(directoryID);

            DataList list = new DataList();
            list.ID = "albumList";
            if (repeatColumns != 0)
                list.RepeatColumns = repeatColumns;
            list.RepeatLayout = RepeatLayout.Table;
            list.RepeatDirection = RepeatDirection.Horizontal;
            list.DataSource = table;

            if (string.IsNullOrEmpty(folderIcon))
            {
                Page thePage = HttpContext.Current.Handler as Page;
                if (thePage != null)
                    folderIcon = thePage.ClientScript.GetWebResourceUrl(typeof(PagerControl), "BEGalleryExtension.Images.folder.png"); ;
            }

            AlbumListTemplate template = new AlbumListTemplate(folderIcon);
            list.ItemTemplate = template;
            list.DataBind();

            StringWriter sw = new StringWriter();
            list.RenderControl(new HtmlTextWriter(sw));

            writer.Write(sw.ToString());
            writer.Write(Environment.NewLine);
        }
    }

    public class AlbumListTemplate : ITemplate
    {
        private string folderIcon;
        public AlbumListTemplate(string icon)
        {
            folderIcon = icon;
        }

        public void InstantiateIn(Control container)
        {
            Image img = new Image();
            img.ImageUrl = folderIcon;

            HyperLink album = new HyperLink();
            album.DataBinding += new EventHandler(album_DataBinding);

            container.Controls.Add(img);
            container.Controls.Add(album);
        }

        void album_DataBinding(object sender, EventArgs e)
        {
            QueryString query = new QueryString();
            HyperLink link = (HyperLink)sender;
            DataListItem item = (DataListItem)link.NamingContainer;
            DataRowView drv = (DataRowView)item.DataItem;
            DataRow row = drv.Row;

            string name = Convert.ToString(row["DirectoryName"]);
            if (name == "Up a level")
                query.RemoveParameter("pg");

            query["AlbumID"] = Convert.ToString(row["ID"]);
            link.Text = Convert.ToString(row["DirectoryName"]);
            link.NavigateUrl = query.All;
        }
    }
}
